package com.sxbang.bms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dao.PermissionDao;
import com.sxbang.bms.dao.RoleDao;
import com.sxbang.bms.dto.RoleDto;
import com.sxbang.bms.model.SysPermission;
import com.sxbang.bms.model.SysRole;
import com.sxbang.bms.service.PermissionService;
import com.sxbang.bms.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao dao;

    @Override
    public SysPermission save(SysPermission sysPermission) {
        dao.save(sysPermission);
        return sysPermission;
    }

    @Override
    public List<SysPermission> save(List<SysPermission> sysPermissions) {
        return null;
    }

    @Override
    public SysPermission get(Integer id) {
        return dao.get(id);
    }

    @Override
    public List<SysPermission> findAll() {
        return dao.findAll();
    }

    @Override
    public List<SysPermission> list(Iterable<Integer> integers) {
        return null;
    }

    @Override
    public List<SysPermission> list(Integer offset, Integer limit) {
        return dao.listByPage(offset, limit);
    }

    @Override
    @Transactional
    public SysPermission update(SysPermission e) {
        dao.update(e);
        return e;
    }

    @Override
    public void delete(SysPermission e) {
        dao.delete(e);
    }

    @Override
    public void delete(Integer id) {
        dao.deleteById(id);
    }

    @Override
    public Long countAll() {
        return dao.countAll();
    }

    @Override
    public Results<SysPermission> listByPage(Integer offset, Integer limit) {
        log.info(getClass().getName() + ".listByPage(" + offset + "," + limit + ")");
        return Results.success(dao.countAll().intValue(), dao.listByPage(offset, limit));
    }

    @Override
    public Results<SysPermission> listByName(String name) {
        return Results.success(0, dao.listByName(name));
    }

    @Override
    public Results<SysPermission> listByNameAndPage(String name, Integer offset, Integer limit) {
        return Results.success(dao.countByName(name).intValue(), dao.listByNameAndPage(name,offset,limit));
    }

    @Override
    public Long countByName(String name) {
        return dao.countByName(name);
    }

    @Override
    public Results<JSONArray> listAllPermission() {
        log.info(getClass().getName() + ".listAllPermission()");
        List<SysPermission> datas = dao.findAll();
        JSONArray array = new JSONArray();
        log.info(getClass().getName() + ".setPermissionsTree(?,?,?)");
        setPermissionsTree(0,datas,array);
        return Results.success(array);
    }

    /**
     * 菜单树
     *
     * @param parentId
     * @param permissionsAll
     * @param array
     */
    private void setPermissionsTree(Integer parentId, List<SysPermission> permissionsAll, JSONArray array){
        for (SysPermission per : permissionsAll) {
            if (per.getParentId().equals(parentId)) {
                String string = JSONObject.toJSONString(per);
                JSONObject parent = (JSONObject) JSONObject.parse(string);
                array.add(parent);

                if (permissionsAll.stream().filter(p -> p.getParentId().equals(per.getId())).findAny() != null) {
                    JSONArray child = new JSONArray();
                    parent.put("child", child);
                    setPermissionsTree(per.getId(), permissionsAll, child);
                }
            }
        }
    }

    @Override
    public Results<SysPermission> listByRoleId(Integer roleId) {
        List<SysPermission> datas = dao.listByRoleId(roleId);
        return Results.success(0,datas);
    }
}
